package com.xgp.tvolatile;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 不保证原子性
 */
public class VDemo2 {

    //使用原子类
    private static AtomicInteger num = new AtomicInteger();

    public  static void add() {
//        num++;
        num.getAndIncrement();      //+1方法
    }

    //理论上应该为20000
    public static  void main(String[] args) {
        for(int i = 1;i <= 20;i++) {
            new Thread(() -> {
                for(int j = 0;j < 1000;j++) {
                    add();
                }
            }).start();
        }

        while (Thread.activeCount() > 2) {
            Thread.yield();
        }

        System.out.println(Thread.currentThread().getName() + " = " + num);
    }
}
